<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/23
 * Time: 14:27
 */

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\LoginAuth;
use EasyWeChat\Foundation\Application;
use zhtfrontend\models\datas\Complaint;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\Praise;
use yii\bootstrap\ActiveForm;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\data\Pagination;


class CustomerpraiseController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = Praise::find()->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = $param['searchData'];
            if (!empty($search_data['name'])) {
                $cids = Customer::find()->select('id')->where(['like', 'name', $search_data['name']])->column();
                $data->andWhere(['customer_id' => $cids]);
            }
            if (!empty($search_data['mechanic_name'])) {
                $mids = Mechanic::find()->select('id')->where(['like', 'name', $search_data['mechanic_name']])->column();
                $data->andWhere(['mechanic_id' => $mids]);
            }
            if (!empty($search_data['goods_name'])) {
                $gids = Goods::find()->select('id')->where(['like', 'name', $search_data['goods_name']])->column();
                $data->andWhere(['goods_id' => $gids]);
            }
        }
        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'business_director') {
            $mids = Mechanic::find()->where(['admin_id' => \Yii::$app->user->id])->column();
            $data->andWhere(['id' => $mids]);
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $list = Fun::asArray($model, ['customer_name', 'mechanic_name', 'teacher_name', 'teacher_name']);

        return $this->render('index', ['data' => $list, 'pages' => $pages]);
    }

    public function actionHandle()
    {
        $id = \Yii::$app->request->get('id');
        if ($id == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = Complaint::findOne($id);
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model->scenario = 'handle';
        if ($model->status == 0) {
            $model->status = 1;
        }

        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->admin_id = \Yii::$app->user->id;
            $model->admin_name = \Yii::$app->user->identity->name;
            $model->updated_at = time();
            $title = '投诉处理';
            $m_name = isset($model->mechanic) ? $model->mechanic->name : '';
            if ($model->save(false)) {
                if ($model->status > 0) {
                    $options = \Yii::$app->params['options'];
                    $row = LoginAuth::find()->where(['user_id' => $model->customer_id])->one();
                    if ($row) {
                        $app = new Application($options);
                        $str = "投诉机构：" . $m_name . "\n处理状态：";
                        if ($model->status == 1) {
                            $str .= "处理中 \n";
                        } elseif ($model->status == 2) {
                            $str .= "处理成功！\n";
                        } elseif ($model->status == 3) {
                            $str .= "投诉驳回 \n";
                        }
                        if (!empty($model->remarks)) {
                            $str .= "描述：" . $model->remarks;
                        }
                        $broadcast = $app->broadcast;
                        $openid = [0 => '', 1 => $row->source_id];
                        $broadcast->send('text', $str, $openid);
//                        $news = new News([
//                            'title' => $title,
//                            'description' => $str,
//                        ]);
//                        $app->staff->message([$news])->to($row->source_id)->send();
                    }
                }
                return $this->redirectMessage('处理成功', Fun::url(['complaint/index']));
            } else {
                return $this->redirectMessage('处理失败', Fun::url(['complaint/index']));
            }
        }

        return $this->render('handle', ['model' => $model, 'service' => $this->service_arr, 'teaching' => $this->teaching_arr, 'environmental' => $this->environmental_arr]);

    }
}
